package com.hex.ds.hdrs.center.service;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.hex.bigdata.hbda.utils.RSAUtil;
import com.hex.ds.hdrs.agent.po.AgentConf;
import com.hex.ds.hdrs.agent.service.AgentConfService;
import com.hex.ds.hdrs.agent.service.AgentManager;
import com.hex.ds.hdrs.agent.service.AgentSession;
import com.hex.ds.hdrs.app.service.AppManager;
import com.hex.ds.hdrs.center.po.CenterConf;
import com.hex.ds.hdrs.center.vo.CronTaskReq;
import com.hex.ds.hdrs.center.vo.HeartBeatReq;
import com.hex.ds.hdrs.center.vo.RegisterReq;
import com.hex.ds.hdrs.center.vo.RegisterRsp;
import com.hex.ds.hdrs.common.base.BaseRsp;
import com.hex.ds.hdrs.common.constant.Const;
import com.hex.ds.hdrs.common.constant.ErrorCode;
import com.hex.ds.hdrs.common.constant.HttpConst;
import com.hex.ds.hdrs.common.exception.BizException;
import com.hex.ds.hdrs.common.utils.HdrsDateUtil;
import com.hex.ds.hdrs.common.utils.IpAddressUtil;
import com.hex.ds.hdrs.common.utils.ListUtil;
import com.hex.ds.hdrs.engine.manager.LstFinishManager;
import com.hex.ds.hdrs.engine.manager.RunningManager;
import com.hex.ds.hdrs.engine.manager.ToDoManager;
import com.hex.ds.hdrs.engine.manager.ToRunManager;
import com.hex.ds.hdrs.log.service.LogService;
import com.hex.ds.hdrs.period.task.vo.CycleTaskReq;
import com.hex.ds.hdrs.period.task.vo.TaskHeartBeatReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.SocketException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Package: com.hex.ds.hdrs.center.service
 * @ClassName CenterManager
 * @Description: 中心 管理
 * @Author: jd.chen
 * @Date 2021/7/5 20:02
 * @Version v1.0
 **/
@Slf4j
@Component
public class CenterManager {
    private static CenterManager instance;

    private CenterManager() {
        instance = this;
    }

    private long lstUpTs = HdrsDateUtil.getMillis(new Date());
    private boolean isMaster = false;
    private String centerStatus = Const.CENTER_SLEEP;
    private CenterConf centerConf = null;
    private CenterConf masterCenter = null;
    private int maxAgentNum = 0;
    private int maxTaskNum = 0;
    private int maxConcurrentNum = 0;
    private boolean clusterFlag = true;

    /**
     * db更新时间失效限制 分钟
     */
    @Value("${center.noUpDbTsLimit}")
    private int noUpDbTsLimit;
    /**
     * 内存中更新时间失效限制 秒
     */
    @Value("${center.noUpTsLimit}")
    private int noUpTsLimit;
    /**
     * 配置文件中中心ip
     */
    @Value("${center.centerIp}")
    private String centerIp;

    @Value("${server.port}")
    private Integer centerPort;

    @Value("${license.encrypt-info}")
    private String licenseEncryptInfo;
    @Value("${license.private-key}")
    private String licensePrivateKey;

    @Autowired
    private ToDoManager toDoManager;
    @Autowired
    private ToRunManager toRunManager;
    @Autowired
    private RunningManager runningManager;
    @Autowired
    private LstFinishManager lstFinishManager;
    @Autowired
    private AppManager appManager;
    @Autowired
    private AgentManager agentManager;
    @Autowired
    private CenterService centerService;
    @Autowired
    private LogService logService;
    @Autowired
    private CenterConfService centerConfService;
    @Autowired
    private AgentConfService agentConfService;

    /*
     * @Method: centerInit <br>
     * @Param: [] <br>
     * @Return: void <br>
     * @Description：中心启动注册<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/27 14:42 <br>
     * @Version： V2.0.2<br>
     */
    public void centerInit() throws SocketException {
        try {
            if (!IpAddressUtil.getIpAddress().contains(centerIp)) {
                throw new BizException(ErrorCode.ERROR_830006);
            }
            log.info("【调度中心启动】-2.1、调度中心信息注册,当前主机ip: {}", centerIp);
            centerConf = centerService.getCenterConf(centerIp);
            if (centerConf == null) {
                CenterConf centerConf = new CenterConf();
                centerConf.setCenterCode("CENTER_" + centerIp);
                centerConf.setCenterName("调度中心_" + centerIp);
                centerConf.setCenterIp(centerIp);
                centerConf.setCenterPort(String.valueOf(centerPort));
                centerConf.setCenterStatus(Const.CENTER_SLEEP);
                centerConfService.add(centerConf);
                log.info("【调度中心启动】-2.2、调度中心_{}未注册，现注册成功！，注册信息:{}", centerIp, centerConf);
            } else if (!String.valueOf(centerPort).equals(centerConf.getCenterPort())) {
                centerConf.setCenterPort(String.valueOf(centerPort));
                centerConfService.update(centerConf);
                log.info("【调度中心启动】-2.2、调度中心_{}端口变更，现变更成功！，变更信息{}", centerIp, centerConf);
            }
        } catch (BizException e) {
            log.error("调度中心启动：中心注册异常, 异常信息: {}", e.getErrMsg(), e);
        } catch (Exception e) {
            log.error("调度中心启动：中心注册异常, 异常信息: {}", e.getMessage(), e);
            throw e;
        }
    }

    /**
     * @Method: agentRegister
     * @Param: [registerReq]
     * @Return: com.hex.ds.hdrs.common.base.BaseRsp
     * @Description： 节点注册
     * @Author： jd.chen
     * @Date： 2021/7/5 19:30
     * @Version： V1.0
     */
    public BaseRsp agentRegister(RegisterReq registerReq) {
        log.info("节点注册【CenterService.agentRegisterDeal】开始======》");
        try {
            // 校验中心是不是master
            this.validMaster();
            // 校验agent是否在中心配置过
            AgentConf agentConf = agentManager.validAgent(registerReq);
            // 校验agent数是否超过限制数
            this.validMaxAgentLimit();
            // 注册agent
            AgentSession agentSession = this.agentManager.crtAgentSession(agentConf.getAgentCode());
            // 邮件发送
            centerService.agentLoginNotice(agentSession);
            // 添加注册成功 运行日志
            logService.addRegisterSuccRunLog(registerReq);

            return new BaseRsp(HttpConst.HTTP_STATUS_OK, HttpConst.REGISTER_SUCC_MSG, this.getMasterCenterIp(),
                    new RegisterRsp(agentSession.getToken(), agentConf.getAgentCode(), agentConf.getAgentName()));
        } catch (BizException e) {
            log.error("节点【agentName：{}，agentIP：{}】注册失败【CenterService.agentRegister】异常：{}",registerReq.getAgentName(),registerReq.getIp(), e.getErrMsg(), e);
            // 添加注册失败 运行日志
            logService.addRegisterFailRunLog(registerReq);
            return new BaseRsp(e.getCode(), e.getErrMsg(), this.getMasterCenterIp(), null);
        }
    }

    /**
     * @Method: agentNotRegistor
     * @Param: [registerReq]
     * @Return: com.hex.ds.hdrs.common.base.BaseRsp
     * @Description： 节点未注册，下线处理
     * @Author： jd.chen
     * @Date： 2021/7/5 19:30
     * @Version： V1.0
     */
    public BaseRsp agentNotRegistor(RegisterReq registerReq) {
        String agentCode = registerReq.getReqCode();
        log.info("节点未注册，下线处理【CenterService.agentNotRegistor】开始======》agentCode: {}", agentCode);
        try {
            // 验证 中心 和 token
            this.checkMasterAndToken(agentCode, registerReq.getToken());
            // DB停止
            centerService.updateAgentStatus(agentCode);
            // 节点未注册 内存中停用
            agentManager.stopAgent(registerReq.getReqCode(), "agent在master中心未注册，agent下线!");
            return new BaseRsp(HttpConst.HTTP_STATUS_OK, "", this.getMasterCenterIp(), null);
        } catch (BizException e) {
            log.error("节点【agentName：{}，agentIP：{}】未注册，下线处理【CenterService.agentNotRegistor】异常: {}",registerReq.getAgentName(),registerReq.getIp(), e.getErrMsg(), e);
            return new BaseRsp(e.getCode(), e.getErrMsg(), this.getMasterCenterIp(), null);
        }
    }

    /**
     * 节点心跳
     *
     * @param heartBeatReq
     */
    public BaseRsp heartBeat(HeartBeatReq heartBeatReq) {
        log.debug("节点心跳【CenterService.heartBeat】开始======》");
        String masterUrl = this.getMasterCenterIp();
        try {
            // 验证 中心 和 token
            this.checkMasterAndToken(heartBeatReq.getReqCode(), heartBeatReq.getToken());
            // 获取agentSession 并验证session是否有效
            AgentSession agentSession = agentManager.checkAgentSession(heartBeatReq.getReqCode());
            //更新数据库中心的心跳时间
            centerService.updateAgentLstTime(agentSession.getAgentCode());
            //添加心跳日志信息表
            this.logService.addAgentHeartbeatLog(heartBeatReq, agentSession);
            //更新内存agentSession的心跳时间
            agentSession.updateHeartBeatTs();
            // agent机器性能告警
            //this.checkAgentPerformance(heartBeatReq);
            return new BaseRsp(HttpConst.HTTP_STATUS_OK, HttpConst.HEART_BEAT_SUCC_MSG, masterUrl, null);
        } catch (BizException e) {
            log.error("节点【agentName：{},agentIP：{}】心跳处理异常【CenterService.heartBeat】:{} ",heartBeatReq.getAgentName(), heartBeatReq.getIp(), e.getErrMsg(), e);
            return new BaseRsp(e.getCode(), e.getErrMsg(), masterUrl, null);
        }
    }

    /**
     * 任务节点心跳
     *
     * @param taskHeartBeatReq
     */
    public BaseRsp taskHeartBeat(TaskHeartBeatReq taskHeartBeatReq) {
        String masterUrl = this.getMasterCenterIp();
        try {
            return new BaseRsp(HttpConst.HTTP_STATUS_OK, HttpConst.TASK_HEART_BEAT_SUCC_MSG, masterUrl, null);
        } catch (BizException e) {
            log.error("任务节点心跳处理异常【CenterService.taskHeartBeat】异常:{} ", e.getErrMsg(), e);
            return new BaseRsp(e.getCode(), e.getErrMsg(), masterUrl, null);
        }
    }

    /**
     * 周期任务执行结果
     *
     * @param req
     */
    public void cycleTaskExecBack(CycleTaskReq req) {
        log.debug("周期任务执行结果【CenterService.cycleTaskExecBack】开始======》");
        try {
            // 验证 中心 和 token
            this.checkMasterAndToken(req.getReqCode(), req.getToken());
            // 结果处理
            centerService.cycleTaskExecBack(req);
        } catch (BizException e) {
            log.error("周期任务【taskCode：{}，业务日期：{}】执行结果【CenterService.cycleTaskExecBack】异常: {}", req.getTaskCode(),req.getBizDate(), e.getErrMsg(), e);

        }
    }

    /**
     * 定时任务执行结果
     *
     * @param req
     */
    public void cronTaskExecBack(CronTaskReq req) {
        log.info("定时任务执行结果【CenterService.cronTaskExecBack】开始======》");
        try {
            // 验证 中心 和 token
            this.checkMasterAndToken(req.getReqCode(), req.getToken());
            // 结果处理
            centerService.cronTaskExecBack(req.getTaskInfo());
        } catch (BizException e) {
            log.error("定时任务【taskCode：{}，业务日期：{}】执行结果【CenterService.cronTaskExecBack】异常:{} ",req.getTaskCode(),req.getBizDate(), e.getErrMsg(), e);

        }
    }

    /**
     * 校验 中心是不是master
     * 校验 agentToken 是否有效
     *
     * @param agentCode
     * @param token
     * @return
     */
    public void checkMasterAndToken(String agentCode, String token) {
        log.debug("校验 中心是不是master,校验 agentToken 是否有效【CenterService.checkMasterAndToken】========》");
        // 校验中心是不是master
        this.validMaster();
        // 校验 agentToken 是否有效
        agentManager.validAgentToken(agentCode, token);
    }

    public String getMasterCenterIp() {
        if (null == masterCenter) {
            log.error("【getMasterCenterIp】中心获取Master信息异常");
            throw new BizException(ErrorCode.ERROR_800018);
        } else {
            return masterCenter.getCenterIp();
        }
    }

    public String getMasterCenterPort() {
        if (null == masterCenter) {
            log.error("【getMasterCenterPort】中心获取Master信息异常");
            throw new BizException(ErrorCode.ERROR_800018);
        } else {
            return masterCenter.getCenterPort();
        }
    }

    /**
     * 验证是否超过最大agent数
     */
    public void validMaxAgentLimit() {
        if (ListUtil.isEmpty(agentManager.getAgentSessionList())) {
            return;
        }
        if (agentManager.getAgentSessionList().size() >= getMaxAgentNum()) {
            log.warn("有效节点数超过限制数【AgentManager.validMaxAgentLimit】");
            throw new BizException(ErrorCode.ERROR_840001);
        }
    }

    /**
     * 中心是否可处理
     *
     * @return
     */
    public boolean isCanDeal() {
        return this.isMaster
                && (Const.CENTER_RUN.equalsIgnoreCase(this.centerStatus) || Const.CENTER_STARTING.equalsIgnoreCase(this.centerStatus));
    }

    /**
     * 非中心抛异常
     */
    public void validMaster() {
        if (!this.isMaster) {
            log.warn("【validMaster】非Master中心");
            throw new BizException(ErrorCode.ERROR_800000);
        }
        if (Const.CENTER_STOPPING.equalsIgnoreCase(centerStatus)) {
            log.warn("【validMaster】中心暂停");
            throw new BizException(ErrorCode.ERROR_800004);
        }
        if (Const.CENTER_STARTING.equalsIgnoreCase(centerStatus)) {
            log.warn("【validMaster】中心启动中");
            throw new BizException(ErrorCode.ERROR_800002);
        }
    }

    /**
     * 初始化 【master】
     */
    public void init() {
        if (!this.canInit()) {
            return;
        }
        log.info("初始化【CenterManager.init】开始==========》中心名称:{}", centerConf.getCenterName());
        try {
            log.info("【1】打标--正在启动中");
            this.centerStatus = Const.CENTER_STARTING;
            this.isMaster = true;
            this.masterCenter = centerConf;
            log.debug("【masterCenter】:{}", JSON.toJSONString(masterCenter));

//            log.info("【3】最新完成队列初始化");
//            lstFinishManager.init();

            log.info("【2】appSession初始化");
            appManager.init(toRunManager);

//            log.info("【5】任务加载器启动初始化");
//            centerService.loaderInit();

            log.info("【3】任务处理器启动初始化线程");
            centerService.processorInit();

            log.info("【4】任务分发器启动初始化线程");
            centerService.dispatcherInit();

            log.info("【5】定时任务初始化");
            centerService.cronTaskEngineInit();

            log.info("【6】打标--正常运行中");
            this.centerStatus = Const.CENTER_RUN;

            log.info("【7】 更新DB 状态");
            centerService.toMaster(centerConf.getCenterCode());

            log.info("【8】通知所有agent  新master初始化完成");
            agentManager.notifyMasterInitSucc(centerConf);

            log.info("【9】邮件通知");
            centerService.centerSwitchNotic(masterCenter.getCenterName());

            log.info("【10】自动重启调度服务");
            agentConfService.restartEnabledAgents();

            log.info("【11】添加中心成为Master成功运行日志");
            logService.centerInitSuccLog(centerConf);

            log.info("【12】任务加载器启动初始化开始");
            centerService.loaderInit();
            log.info("【12】任务加载器启动初始化完成");

        } catch (Exception e) {
            log.error("初始化【CenterManager.init】异常:{}", e.getMessage(), e);
            logService.centerInitFailLog(centerConf);
            this.downMaster(HdrsDateUtil.getMillis(new Date()));
        } finally {
            log.info("初始化【CenterManager.init】结束《==========中心名称:{}", centerConf.getCenterName());
        }
    }

    /**
     * 原master 下线
     */
    public void downMaster(long doDownTs) {
        if (!this.canDownCenter(doDownTs)) {
            log.warn("【警告】【CenterManager.downMaster】不允许master下线………………");
            return;
        }
        log.info("原master 下线【CenterManager.downMaster】开始=====》中心: {}", centerConf.getCenterName());
        try {
            log.info("【1】修改状态--开始");
            this.centerStatus = Const.CENTER_STOPPING;
            this.isMaster = false;
            log.info("【1】修改状态--结束");

            log.info("【2】agentSession appSession 停--开始");
            agentManager.clearAgentSession();
            appManager.clearAppSession();
            log.info("【2】agentSession appSession 停--结束");

            log.info("【3】相关处理器线程停--开始");
            this.stopExec();
            log.info("【3】相关处理器线程停--结束");

            log.info("【4】内存对象清空--开始");
            this.clearMemory();
            log.info("【4】内存对象清空--结束");

            log.info("【5】更新DB--开始");
            centerService.notMaster(centerConf.getCenterCode());
            log.info("【5】更新DB--结束");

            log.info("【6】中心从MASTER下线--开始");
            this.logService.centerDownSuccLog(centerConf);
            log.info("【6】中心从MASTER下线--结束");
        } catch (BizException e) {
            log.error("【master下线】异常:{}", e.getErrMsg(), e);
            this.logService.centerDownFailLog(centerConf);
        } catch (Exception e) {
            log.error("【master下线】异常:{}", e.getMessage(), e);
            this.logService.centerDownFailLog(centerConf);
        } finally {
            this.centerStatus = Const.CENTER_SLEEP;
            this.isMaster = false;
            log.info("原master 下线【CenterManager.downMaster】结束《=====中心: {}", centerConf.getCenterName());
        }
    }

    /**
     * 中心初始化配置
     */
    public void initConf() {
        log.info("中心初始化配置【CenterManager.initConf】开始==========》");
        centerConf = centerService.getCenterConf(centerIp);
        centerService.initMaster(centerConf);
    }

    /**
     * @Method: confMaxAgentAndTask
     * @Param: []
     * @Return: void
     * @Description： 配置 最大agent数  和  最大任务数
     * @Author： jd.chen
     * @Date： 2021/6/24 11:41
     * @Version： V1.0
     */
    public void confMaxAgentAndTask() {
        try {
            String licenseInfo = RSAUtil.rsaDecrypt(licensePrivateKey, licenseEncryptInfo);
            JSONObject licenseJson = JSONUtil.parseObj(licenseInfo);
            maxAgentNum = (int) licenseJson.get("agentNum");
            maxTaskNum = (int) licenseJson.get("taskNum");
            maxConcurrentNum = (int) licenseJson.get("concurrentNum");
            clusterFlag = (boolean) licenseJson.get("clusterFlag");
        } catch (Exception e) {
            log.error("【confMaxAgentAndTask】获取license异常: {}", e.getMessage(), e);
            throw new BizException(ErrorCode.ERROR_830003);
        }
    }

    /**
     * 相关处理器线程停  【master】
     */
    public void stopExec() {
        log.info("相关处理器线程停【CenterManager.stopExec】开始==========》");
        toDoManager.setMark(Const.STOP_LOADER_MARK);
        centerService.closeProcess();
        centerService.closeDispatcher();
        lstFinishManager.setMark(Const.STOP_LST_FNS_MARK);
    }

    /**
     * 内存对象清空  【master】
     */
    public void clearMemory() {
        log.info("内存对象清空【CenterManager.clear】开始==========》");
        toDoManager.clear();
        toRunManager.clear();
        runningManager.clear();
        lstFinishManager.clear();
    }

    /**
     * 抢占DB
     */
    public void grabDb() {
        log.debug("抢占DB【CenterManager.grabDb】开始========》");
        if (null == centerConf) {
            initConf();
        }

        String centerCode = centerConf.getCenterCode();
        if (this.centerStopping()) {
            log.warn("【警告】【{}】中心正在从master切换成休眠状态，不参与DB抢占………………", centerCode);
            return;
        }

        long grabTs = HdrsDateUtil.getMillis(new Date());
        try {
            boolean upFlag = this.centerService.grabDb(centerConf, noUpDbTsLimit);
            this.lstUpTs = grabTs;
            if (upFlag) {
                //其他中心休眠
                this.centerService.otherNotMaster(centerConf.getCenterCode());
                this.init();
            } else {
                this.downMaster(grabTs);
            }
        } catch (Exception e) {
            log.error("【异常】【CenterManager.grabDb】: {}", e.getMessage(), e);
            this.downMaster(grabTs);
        }
    }

    /**
     * @Method: singleCenterInit
     * @Param: []
     * @Return: void
     * @Description： 单机版中心初始化
     * @Author： jd.chen
     * @Date： 2021/6/30 18:22
     * @Version： V1.0
     */
    public void singleCenterInit() {
        log.debug("【CenterManager.singleCenterInit】开始========》");
        if (null == this.centerConf) {
            //中心配置初始化
            this.initConf();
        }

        if (!this.isMaster) {
            // 中心启动初始化
            this.init();
        }
    }

    /**
     * 判断是否允许中心从master下线
     *
     * @return
     */
    public boolean canDownCenter(long doDownTs) {
        log.info("判断是否允许中心从master下线【CenterManager.canDownCenter】开始========》");
        if (!this.isMaster) {
            log.warn("【警告】【CenterManager.canDownCenter】该中心不是master,不需要下线………………");
            return false;
        }
        if (doDownTs - lstUpTs <= noUpTsLimit) {
            log.warn("【警告】【CenterManager.canDownCenter】master中心正常活跃,不需要下线………………");
            return false;
        }
        return true;
    }

    /**
     * 判断是否允许初始化
     *
     * @return
     */
    public boolean canInit() {
        log.debug("判断是否允许初始化【CenterManager.canInit】开始========》");
        if (this.isMaster) {
            log.debug("【CenterManager.canInit】该中心已经是master,不允许初始化………………");
            return false;
        }
        if (Const.CENTER_STARTING.equalsIgnoreCase(this.centerStatus)) {
            log.debug("【CenterManager.canInit】该中心正在成为master中,不允许初始化………………");
            return false;
        }
        return true;
    }

    /**
     * 中心是否停止判断
     *
     * @return
     */
    public boolean centerStopping() {
        log.debug("中心是否停止判断【CenterManager.centerStopping】");
        return Const.CENTER_STOPPING.equalsIgnoreCase(this.centerStatus);
    }

    /**
     * @Method: getNoMasterCenter
     * @Param: []
     * @Return: java.util.List<com.hex.ds.hdrs.center.po.CenterConf>
     * @Description： 获取非 master 中心
     * @Author： jd.chen
     * @Date： 2021/7/5 19:45
     * @Version： V1.0
     */
    public List<CenterConf> getNoMasterCenter() {
        log.info("获取非 master 中心【CenterService.getNoMasterCenter】");
        Map map = new HashMap(16);
        map.put("isMaster", Const.NOT_MASTER);
        return centerService.getNoMasterCenter(map);
    }

    /**
     * @Method: getMasterValidAgentNum
     * @Param: []
     * @Return: int
     * @Description： 获取有效的节点数
     * @Author： jd.chen
     * @Date： 2021/7/5 19:45
     * @Version： V1.0
     */
    public int getMasterValidAgentNum() {
        log.info("获取【master】有效的节点数【CenterService.getMasterValidAgentNum】");
        return agentManager.getValidAgentNum();
    }

    /**
     * @Method: getValidAgentNum
     * @Param: []
     * @Return: int
     * @Description： 获取有效的节点数
     * @Author： jd.chen
     * @Date： 2021/7/5 19:52
     * @Version： V1.0
     */
    public int qryValidAgentNum() {
        log.info("获取有效的节点数【CenterService.getValidAgentNum】");
        int validAgentNum = 0;
        List<AgentConf> agentConfList = centerService.qryAgentConfList();
        for (AgentConf agentConf : agentConfList) {
            if (this.agentManager.testLink(agentConf)) {
                validAgentNum++;
            }
        }
        return validAgentNum;
    }

    /**
     * @Method: upValidAgentNum
     * @Param: [validAgentNum, centerCode]
     * @Return: void
     * @Description： 更新有效节点数
     * @Author： jd.chen
     * @Date： 2021/7/5 19:52
     * @Version： V1.0
     */
    public void upValidAgentNum(int validAgentNum, String centerCode) {
        log.info("更新有效节点数【CenterService.upValidAgentNum】");
        Map map = new HashMap(16);
        map.put("validAgentNum", validAgentNum);
        map.put("centerCode", centerCode);
        centerService.updateCenterConf(map);
    }

    /**
     * @Method: ramQueueQuantityStatistics
     * @Param: []
     * @Return: void
     * @Description： 记录内存队列现存作业数
     * @Author： jd.chen
     * @Date： 2021/7/5 19:52
     * @Version： V1.0
     */
    public void ramQueueQuantityStatistics() {
        String nowTime = HdrsDateUtil.getCurrentTime();
        this.centerService.ramQueueQuantityStatisticsByAppCode(this.toDoManager.getToDoJobList(), Const.QUEUE_INF_TO_DO, nowTime);
        this.centerService.ramQueueQuantityStatisticsByAppCode(this.toRunManager.getToRunJobList(), Const.QUEUE_INF_TO_RUN, nowTime);
        this.centerService.ramQueueQuantityStatisticsByAppCode(this.runningManager.getRunJobList(), Const.QUEUE_INF_RUNNING, nowTime);
    }

    public String getCenterCode() {
        return centerConf.getCenterCode();
    }

    public String getCenterIp() {
        return centerConf.getCenterIp();
    }

    public CenterConf getCenter() {
        return centerConf;
    }

    public int getValidAgentNum() {
        return centerConf.getValidAgentNum();
    }

    public boolean isMaster() {
        return isMaster;
    }

    public int getMaxAgentNum() {
        return maxAgentNum;
    }

    public void setMaxAgentNum(int maxAgentNum) {
        this.maxAgentNum = maxAgentNum;
    }

    public void setMaxTaskNum(int maxTaskNum) {
        this.maxTaskNum = maxTaskNum;
    }

    public int getMaxTaskNum() {
        return maxTaskNum;
    }

    public int getMaxConcurrentNum() {
        return maxConcurrentNum;
    }

    public void setMaxConcurrentNum(int maxConcurrentNum) {
        this.maxConcurrentNum = maxConcurrentNum;
    }

    public boolean isClusterFlag() {
        return clusterFlag;
    }

    public void setClusterFlag(boolean clusterFlag) {
        this.clusterFlag = clusterFlag;
    }


}
